package com.lss.loginregister.activity;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.lss.loginregister.R;
import com.lss.loginregister.adapter.ViewPagerAdapter;

public class HomeActivity extends AppCompatActivity {
    private ViewPager2 viewPager;
    private BottomNavigationView bottomNav;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_home);
        viewPager = findViewById(R.id.vp_bottom_nav);
        bottomNav = findViewById(R.id.bottom_nav);

        // 设置ViewPager适配器
        viewPager.setAdapter(new ViewPagerAdapter(this));

        // 联动ViewPager和底部导航
        bottomNav.setOnItemSelectedListener(item -> {
            int id = item.getItemId();
            if (id == R.id.nav_data_transmit) {
                viewPager.setCurrentItem(0);
            } else if (id == R.id.nav_json_parse) {
                viewPager.setCurrentItem(1);
            } else if (id != R.id.nav_note) {
                viewPager.setCurrentItem(2);
            }
            return true;
        });

        // ViewPager页面切换监听
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0: bottomNav.setSelectedItemId(R.id.nav_data_transmit); break;
                    case 1: bottomNav.setSelectedItemId(R.id.nav_json_parse); break;
                    case 2: bottomNav.setSelectedItemId(R.id.nav_note); break;
                }
            }
        });
    }
}
